/*
 * GoodsGridProvider.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.widget.dialog.QuickBuyDialog;
import com.yidiandao.business.ui.widget.view.FlagView;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.RegexUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 单行显示两个商品的Provider
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/2 11:04
 */
public class GoodsGridProvider extends ItemViewProvider<DoubleGoods, GoodsGridProvider.ViewHolder> {

    private Activity mActivity;

    public GoodsGridProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.item_home_goods_grid, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull DoubleGoods doubleGoods) {

        final Goods goods1 = doubleGoods.getLeftGoods();
        final Goods goods2 = doubleGoods.getRightGoods();

        refreshUI1(holder, goods1);
        initListeners1(holder, goods1);

        // 右边商品
        if (goods2 == null) {
            holder.llytGoods2.setVisibility(View.INVISIBLE);
            return;
        }

        holder.llytGoods2.setVisibility(View.VISIBLE);
        refreshUI2(holder, goods2);
        initListeners2(holder, goods2);
    }

    private void initListeners2(ViewHolder holder, final Goods goods2) {
        holder.ivGoodsImage2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入商品详情
                UmengUtils.onEvent(mActivity, R.string.umeng_home_to_goods_detail);
                GoodsDetailActivity.actionStart(mActivity, goods2.getGoodsId());
            }
        });

        holder.ivShoppingCart2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 已登录，获取商品购物车信息
                getGoodsCartInfo(goods2);
            }
        });
    }

    private void initListeners1(ViewHolder holder, final Goods goods1) {
        holder.ivGoodsImage1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入商品详情
                UmengUtils.onEvent(mActivity, R.string.umeng_home_to_goods_detail);
                GoodsDetailActivity.actionStart(mActivity, goods1.getGoodsId());
            }
        });

        holder.ivShoppingCart1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 已登录，获取商品购物车信息
                getGoodsCartInfo(goods1);
            }
        });
    }

    private void refreshUI1(ViewHolder holder, Goods goods1) {
        ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivGoodsImage1, goods1.getGoodsThumb());
        holder.tvGoodsName1.setText(goods1.getGoodsName());
        holder.flagView1.setFlagText(goods1.getFavourableNotice(), goods1.getFavourableType());

        if (TextUtils.isEmpty(goods1.getSupplierName())) {
            holder.tvShopName1.setVisibility(View.GONE);
        } else {
            holder.tvShopName1.setVisibility(View.VISIBLE);
            holder.tvShopName1.setText(goods1.getSupplierName());
        }

        String goodsNumberDesc1 = goods1.getGoodsNumberDesc();
        boolean isNumber1 = RegexUtils.isFloat(goodsNumberDesc1);

        holder.tvGoodsStock1.setText(isNumber1 ? UIUtils.getString(R.string.stock_hint_unit, goodsNumberDesc1, goods1.getGoodsUnit())
                : UIUtils.getString(R.string.stock_hint_unit_1, goodsNumberDesc1));

        String singlePrice1 = goods1.getAvgShopPriceFloat();
        if (RegexUtils.isFloat(singlePrice1) && Float.parseFloat(singlePrice1) == 0) {
            // 单瓶价等于0，不显示单瓶价格
            holder.tvSingle1.setVisibility(View.INVISIBLE);
        } else {
            // 显示单瓶价格
            // 显示库存layout
            holder.tvSingle1.setVisibility(View.VISIBLE);
            // 库存layout显示库存的内容
            holder.tvSingle1.setText(getSingePrice(singlePrice1));

        }
        holder.tvGoodsPrice1.setText(TextRenderUtils.getBeautyPrice(goods1.getShopPriceFloat(), goods1.getGoodsUnit(), UIUtils.dp2px(18), UIUtils.dp2px(14)));

        int stockNumber1 = goods1.getGoodsNumber();
        holder.tvSellOut1.setVisibility(stockNumber1 > 0 ? View.GONE : View.VISIBLE);
        holder.ivShoppingCart1.setVisibility(stockNumber1 > 0 ? View.VISIBLE : View.GONE);
    }

    private SpannableStringBuilder getSingePrice(String singlePrice) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        SpannableString pre = TextRenderUtils.getAbsoluteSizeText(UIUtils.getString(R.string.single_hint), UIUtils.dp2px(13));
        SpannableString suf = TextRenderUtils.getBeautyPrice(singlePrice, null, UIUtils.dp2px(15), UIUtils.dp2px(12));

        builder.append(pre);
        builder.append(suf);

        return builder;
    }

    private void refreshUI2(ViewHolder holder, Goods goods2) {
        ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivGoodsImage2, goods2.getGoodsThumb());
        holder.tvGoodsName2.setText(goods2.getGoodsName());
        holder.flagView2.setFlagText(goods2.getFavourableNotice(), goods2.getFavourableType());
        if (TextUtils.isEmpty(goods2.getSupplierName())) {
            holder.tvShopName2.setVisibility(View.GONE);
        } else {
            holder.tvShopName2.setVisibility(View.VISIBLE);
            holder.tvShopName2.setText(goods2.getSupplierName());
        }

        String goodsNumberDesc2 = goods2.getGoodsNumberDesc();
        boolean isNumber2 = RegexUtils.isFloat(goodsNumberDesc2);

        holder.tvGoodsStock2.setText(isNumber2 ? UIUtils.getString(R.string.stock_hint_unit, goodsNumberDesc2, goods2.getGoodsUnit())
                : UIUtils.getString(R.string.stock_hint_unit_1, goodsNumberDesc2));

        String singlePrice2 = goods2.getAvgShopPriceFloat();
        if (RegexUtils.isFloat(singlePrice2) && Float.parseFloat(singlePrice2) == 0) {
            // 单瓶价等于0，不显示单瓶价格
            holder.tvSingle2.setVisibility(View.INVISIBLE);
        } else {
            // 显示单瓶价格
            holder.tvSingle2.setVisibility(View.VISIBLE);
            // 库存layout显示库存的内容
            holder.tvSingle2.setText(getSingePrice(singlePrice2));
        }
        holder.tvGoodsPrice2.setText(TextRenderUtils.getBeautyPrice(goods2.getShopPriceFloat(), goods2.getGoodsUnit(), UIUtils.dp2px(18), UIUtils.dp2px(14)));

        int stockNumber2 = goods2.getGoodsNumber();
        holder.tvSellOut2.setVisibility(stockNumber2 > 0 ? View.GONE : View.VISIBLE);
        holder.ivShoppingCart2.setVisibility(stockNumber2 > 0 ? View.VISIBLE : View.GONE);
    }

    private void getGoodsCartInfo(final Goods goods) {


        // 获取商品的购物车信息
        float goodsPrice = Float.parseFloat(goods.getShopPriceFloat());
        int goodsNumber = 1;
        int stockNumber = goods.getGoodsNumber();
        String goodsId = goods.getGoodsId();
        int minNumber = goods.getMinNumber();

        // 最少起订量大于库存,无法购买
        if (minNumber > stockNumber) {
            UIUtils.showToast(UIUtils.getString(R.string.can_not_buy_when_out_of_stock));
            return;
        }

        Goods newGoods = new Goods();
        newGoods.setGoodsId(goodsId);
        newGoods.setStockNumber(stockNumber);
        newGoods.setGoodsName(goods.getGoodsName());
        newGoods.setGoodsPrice(goodsPrice);
        newGoods.setGoodsNumber(goodsNumber);
        newGoods.setLimitNumber(goods.getLimitNumber());
        newGoods.setMinNumber(minNumber);
        // 没有优惠活动，则显示立即立即购买，否则不显示立即购买
        newGoods.setFavourableNotice(goods.getFavourableNotice());

        showAddToCartDialog(newGoods);
    }

    private void showAddToCartDialog(Goods goods) {

        QuickBuyDialog dialog = new QuickBuyDialog(mActivity);
        dialog.show();

        dialog.setGoods(goods);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivGoodsImage1;
        private TextView tvGoodsName1;
        private TextView tvGoodsPrice1;
        private TextView tvGoodsStock1;
        private TextView tvSingle1;
        private ImageView ivShoppingCart1;
        private FlagView flagView1;
        private TextView tvShopName1;
        private TextView tvSellOut1;

        private LinearLayout llytGoods2;
        private ImageView ivGoodsImage2;
        private TextView tvGoodsPrice2;
        private TextView tvGoodsStock2;
        private TextView tvSingle2;
        private FlagView flagView2;
        private TextView tvGoodsName2;
        private ImageView ivShoppingCart2;
        private TextView tvShopName2;
        private TextView tvSellOut2;

        private ViewHolder(View itemView) {
            super(itemView);

            // 左边商品
            ivGoodsImage1 = (ImageView) itemView.findViewById(R.id.iv_goods_image_1);
            flagView1 = (FlagView) itemView.findViewById(R.id.flag_view_1);
            tvGoodsName1 = (TextView) itemView.findViewById(R.id.tv_goods_name_1);
            tvGoodsPrice1 = (TextView) itemView.findViewById(R.id.tv_goods_price_1);
            tvSingle1 = (TextView) itemView.findViewById(R.id.tv_single_1);
            tvGoodsStock1 = (TextView) itemView.findViewById(R.id.tv_goods_stock_1);
            ivShoppingCart1 = (ImageView) itemView.findViewById(R.id.iv_shopping_cart_1);
            tvShopName1 = (TextView) itemView.findViewById(R.id.tv_shop_name_1);
            tvSellOut1 = (TextView) itemView.findViewById(R.id.tv_sell_out_1);

            // 右边商品
            llytGoods2 = (LinearLayout) itemView.findViewById(R.id.llyt_goods_2);
            ivGoodsImage2 = (ImageView) itemView.findViewById(R.id.iv_goods_image_2);
            flagView2 = (FlagView) itemView.findViewById(R.id.flag_view_2);
            tvGoodsName2 = (TextView) itemView.findViewById(R.id.tv_goods_name_2);
            tvGoodsPrice2 = (TextView) itemView.findViewById(R.id.tv_goods_price_2);
            tvSingle2 = (TextView) itemView.findViewById(R.id.tv_single_2);
            tvGoodsStock2 = (TextView) itemView.findViewById(R.id.tv_goods_stock_2);
            ivShoppingCart2 = (ImageView) itemView.findViewById(R.id.iv_shopping_cart_2);
            tvShopName2 = (TextView) itemView.findViewById(R.id.tv_shop_name_2);
            tvSellOut2 = (TextView) itemView.findViewById(R.id.tv_sell_out_2);

        }
    }
}
